<?php

namespace app\admin\controller;

use hg\apidoc\annotation as Apidoc;
use hg\apidoc\annotation\Param;
use hg\apidoc\annotation\Query;
use hg\apidoc\annotation\Returned;
use hg\apidoc\annotation\Header;
use app\BaseController;
use think\facade\View;
use think\facade\Db;
use think\response\Json;
/**
 * 聊天提示词_会员
 *
 * admin-ChatPromptMember
 *
 * @Apidoc\Title("聊天提示词_会员")
 * @Apidoc\Group("api")
 */
class ChatPromptMember extends BaseController
{
    /**
     * 新增
     *
     * admin-ChatPromptMember-add
     */
    public function add()
    {
        if (request()->isPost()) {
            $data = input();
            $data = group_by_table_name($data);
            \app\common\model\ChatPromptMember::create($data['chat_prompt_member']);
            return success();
        } else {
            $options = [];
            $chat_prompt_member__member_id_option = \app\common\model\Member::column('name as name, id as id');
            $options['chat_prompt_member__member_id_options'] = $chat_prompt_member__member_id_option;
            $chat_prompt_member__chat_prompt_id_option = \app\common\model\ChatPrompt::column('name as name, id as id');
            $options['chat_prompt_member__chat_prompt_id_options'] = $chat_prompt_member__chat_prompt_id_option;
            View::assign($options);
            return View::fetch();
        }
    }
    /**
     * 修改
     *
     * admin-ChatPromptMember-update
     */
    public function update()
    {
        if (request()->isPost()) {
            $data = input();
            $data = group_by_table_name($data);
            \app\common\model\ChatPromptMember::update($data['chat_prompt_member']);
            return success();
        } else {
            $data = \app\common\model\ChatPromptMember::find(input('chat_prompt_member__id'));
            $r = [];
            foreach ($data->getData() as $key => $val) {
                $r['chat_prompt_member__' . $key] = $val;
            }
            View::assign('r', $r);
            $options = [];
            $chat_prompt_member__member_id_option = \app\common\model\Member::column('name as name, id as id');
            $options['chat_prompt_member__member_id_options'] = $chat_prompt_member__member_id_option;
            $chat_prompt_member__chat_prompt_id_option = \app\common\model\ChatPrompt::column('name as name, id as id');
            $options['chat_prompt_member__chat_prompt_id_options'] = $chat_prompt_member__chat_prompt_id_option;
            View::assign($options);
            return View::fetch();
        }
    }
    /**
     * 删除
     *
     * admin-ChatPromptMember-delete
     */
    public function delete()
    {
        \app\common\model\ChatPromptMember::destroy(input('chat_prompt_member__id'));
        return success();
    }
    /**
     * 列表
     *
     * admin-ChatPromptMember-list
     */
    public function list()
    {
        if (request()->isPost()) {
            return error('请求方式错误');
        }
        $query = Db::table('db_chat_prompt_member');
        $query->join('db_member', 'db_member.id = db_chat_prompt_member.member_id');
        $query->join('db_chat_prompt', 'db_chat_prompt.id = db_chat_prompt_member.chat_prompt_id');
        if (input('chat_prompt_member__name', '') !== '') {
            $query->where('db_chat_prompt_member.name', 'like', '%' . input('chat_prompt_member__name', '', 'search_in') . '%');
        }
        if (input('member__id', '') !== '') {
            $query->where('db_member.id', '=', input('member__id', '', 'search_in'));
        }
        if (input('chat_prompt__id', '') !== '') {
            $query->where('db_chat_prompt.id', '=', input('chat_prompt__id', '', 'search_in'));
        }
        $query->where('db_chat_prompt_member.delete_time', '=', null);
        $query->where('db_member.delete_time', '=', null);
        $query->where('db_chat_prompt.delete_time', '=', null);
        $query->field(['db_chat_prompt_member.id' => 'chat_prompt_member__id']);
        $query->field(['db_chat_prompt_member.name' => 'chat_prompt_member__name']);
        $query->field(['db_member.name' => 'member__name']);
        $query->field(['db_chat_prompt.name' => 'chat_prompt__name']);
        $query->order('db_chat_prompt_member.id', 'desc');
        $data = $query->paginate(input('listRow') ?: 15);
        $data = ['data' => $data->items(), 'paginate' => $data->render()];
        View::assign($data);
        $options = [];
        $member__id_option = \app\common\model\Member::column('name as name, id as id');
        $options['member__id_options'] = $member__id_option;
        $chat_prompt__id_option = \app\common\model\ChatPrompt::column('name as name, id as id');
        $options['chat_prompt__id_options'] = $chat_prompt__id_option;
        View::assign($options);
        return View::fetch();
    }
}